//app.js
const http = require('./utils/http')
App({
    onLoad: function () {

    },
    onReady: function () {

    },
    onShow: function () {

    },
    onLaunch: function () { //监听小程序初始化
        wx.setTopBarText({text: "优惠券"});//用户置顶后显示的名称
        // 登录
        let openid = wx.getStorageSync("openid");
        if (!openid) {
            wx.login({
                success: res => {
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    if (res.code) {
                        http._get('/mini/login?code=' + res.code).then(res => {
                            this._openid = res.result.openid;
                            this._sessionKey = res.result.sessionKey;
                            this.isAuth(res.result.openid);
                            wx.setStorageSync("openid", res.result.openid);
                            wx.setStorageSync("sessionKey", res.result.sessionKey);
                        }).catch(e => {
                            console.log(e)
                        });

                    } else {
                        console.log('获取用户登录态失败！' + res.errMsg);
                    }
                }
            })
        }


    },
    _openid: '',
    _sessionKey: '',
    getOpenid: function () {
        return this._openid;
    },
    getSessionKey: function () {
        return this._sessionKey;
    },
    getUserInfoAll: function () {//获取全部用户信息
        return {
            'msisdn': wx.getStorageSync('msisdn') || '',
            'address': wx.getStorageSync('address') || '',
            'areaCode': wx.getStorageSync('areaCode') || '',
            'nickName': wx.getStorageSync('nickName') || '',
            'avatarUrl': wx.getStorageSync('avatarUrl') || '',
            'driverType': wx.getStorageSync('driverType') || 0,
            'subjectType': wx.getStorageSync('subjectType') || 1
        };
    },
    getUserInfo: function (key) { // 获取某项用户信息
        return wx.getStorageSync(key);
    },
    setUserInfoAll(values) {// 设置全部用户信息
        wx.setStorageSync('msisdn', values['msisdn'] || '');
        wx.setStorageSync('address', values['address'] || '');
        wx.setStorageSync('areaCode', values['areaCode'] || '');
        wx.setStorageSync('nickName', values['nickName'] || '');
        wx.setStorageSync('avatarUrl', values['avatarUrl'] || '');
        wx.setStorageSync('driverType', values['driverType'] || 0);
        wx.setStorageSync('subjectType', values['subjectType'] || 1);
    },
    setUserInfo(key, value) {// 设置一项用户信息
        wx.setStorageSync(key, value);
    },
    globalData: {
        userInfo: null
    }
})
